Ext.define("core.oa.controller.BorrowMoneyController", {
	extend : "Ext.app.Controller",
	mixins : {
		gridUtils : "core.utils.GridUtils",
		wfUtils : "core.utils.WfUtils",
		formUtils : "core.utils.FormUtils"
	},
	
	init : function() {
		var self = this;
		/**
		 * 显示任务处理的窗口
		 */
		this.showTaskWindow=function(btn){
				//弹出提交确认对话框
				var responObj=btn;
				//数据id
				var idValue=btn.up("borrowform").getForm().findField("id").getValue();
				var win=Ext.create("core.oa.view.WfTaskSubmitWindow", {
					responseObj:responObj,
					idValue:idValue,
					hidden:false,
					taskconmentsHide:false,
					multiSelect:false
				}).show();
				//提交到下一步才显示人员信息
				if(responObj.ref=="wfNext"){
					var store = win.down("roleusertree").getStore();
					store.getProxy().extraParams = {
						assigneType : responObj.assigneType,
						assignees : responObj.assignes,
						roles :responObj.roles
					};
				    store.load();
				}else{
					win.down("roleusertree").hide();
				}
		},
		
		this.control({
			/**
			 * 双击进卡片
			 */
			"borrowgrid" : {
				itemdblclick : function(grid, record, item, index, e, eOpts) {
					var form = grid.up("borrowlayout").down("borrowform");
					// 还原表单内容
					self.resetFormInfo(form);
					form = grid.up("borrowlayout").down("borrowform");
					var formObj = form.getForm();
					grid = form.up("borrowlayout").down("borrowgrid");
					var records = grid.getSelectionModel().getSelection();
					var obj = records[0].data;
					// 把对象值放入form中
					self.setFormValue(formObj, obj);
					self.loadInfo(form, "com.oa.model.BorrowMoney", "id",
							obj.id);
					self.findHistory(form, "com.oa.model.BorrowMoney", obj.id);
					grid.hide();
					form.show();
				}
			},
			/**
			 * 卡片返回列表
			 */
			"borrowform button[ref=return]" : {
				click : function(btn) {
					var form = btn.up("borrowform");
					var grid = form.up("borrowlayout").down("borrowgrid");
					self.hideWfButtons(form);
					form.hide();
					grid.show();
				}
			},
			//TODO
			"borrowgrid button[ref=add]" : {
				click : function(btn) {
					var form = btn.up("borrowlayout").down("borrowform");
					self.resetFormInfo(form);
					form = btn.up("borrowlayout").down("borrowform");
					var formObj = form.getForm();
					formObj.reset();
					grid = form.up("borrowlayout").down("borrowgrid");
					//显示所有工作流按钮
					self.showWfButtons(form);
					//显示页面信息
					grid.hide();
					form.show();
							
				}
			},
			//提交
			"borrowform button[ref=submitSave]" : {
				click : function(btn) {
					var form = btn.up("borrowform");
					//self.submitFlow(form,btn);
					if (!form) {
						alert("传入参数失败");
						return;
					}
					var formObj = form.getForm();
					//数据id
					//TODO--保存情况下
					var idValue = formObj.findField("id").getValue();
					var pfModelName='com.oa.model.BorrowMoney';
					var pfIdName='id';
					//获取人员选择信息
					Ext.Ajax.request({
						url : APPBASE + "pc/wfProcessAction!submitFlow.action",
						params : {
							pfModelName : pfModelName,
							pfIdName : pfIdName,
							idValue : idValue
						},
						method : "POST",
						async : false,
						timeout : 4000,
						success : function(response, opts) {
							var resObj = Ext.JSON.decode(response.responseText);
							if (resObj.success) {
								//弹出提交确认对话框
								var responObj=resObj.obj;
								var win=Ext.create("core.oa.view.WfTaskSubmitWindow", {
									responseObj:responObj,
									idValue:idValue,
									hidden:false,
									taskconmentsHide:true,
									multiSelect:false
								}).show();
								var store = win.down("roleusertree").getStore();
								store.getProxy().extraParams = {
									assigneType : responObj.assigneType,
									assignees : responObj.assignes,
									roles :responObj.roles
								};
								store.load();
							}
						}
					});	
				}
			},
			//tijiao
			"wfTaskSubmitWindow panel[ref=taskjob] button[ref=wfNext]":{
				click:function(btn){
					var form=Ext.getCmp("borrowform");
					var formObj=form.getForm();
					var modelId=formObj.findField("id").getValue();
					alert(modelId);
					self.doSubmit(btn,"com.oa.model.BorrowMoney",modelId,{form:form,modelName:"com.oa.model.BorrowMoney",idName:"id"});
				}
			},
			
			
			"borrowgrid button[ref=delete]" : {
				click : function(btn) {
					var grid = self.findGrid(btn);
					self.doRemove(grid, "id", APPBASE + "pc/borrowMoneyAction");
				}
			},
			"borrowform button[ref=save]" : {
				click : function(btn) {
					var form = btn.up("borrowlayout").down("borrowform");
					var grid = form.up("borrowlayout").down("borrowgrid");
					var formObj = form.getForm();
					var id = formObj.findField("id").getValue();
					var ActionName = APPBASE + 'pc/borrowMoneyAction!doSave.action';
					var msg = "保存成功";
					if (id != null && id != "") {
						ActionName = APPBASE + 'pc/borrowMoneyAction!doUpdate.action';
						msg = "更新成功";
					}
					form.submit({
						clientValidation : true,
						url : ActionName,
						success : function(form, action) {
							var resObj = Ext
									.decode(action.response.responseText);
							if (resObj.success) {
								var obj = resObj.obj;
								form.findField("createUser")
										.setValue(obj.createUser);
								form.findField("createUserCode")
										.setValue(obj.createUserCode);
								form.findField("createDept")
										.setValue(obj.createDept);
								form.findField("createDeptCode")
										.setValue(obj.createDeptCode);
								form.findField("createTime")
										.setValue(obj.createTime);
								grid.getStore().load();
								Ext.Msg.alert("提示", msg);
							} else {
								Ext.Msg.alert("提示", resObj.obj);
							}
						},
						failure : function(form, action) {
							Ext.Msg.alert("提示",Ext.decode(action.response.reponseText).obj);
						}
					});
				}
			},
			"borrowform button[ref=wfStart]" : {
				click : function(btn) {
					var form = btn.up("borrowform");
					var formObj = form.getForm();
					var idValue = formObj.findField("id").getValue();
					var processFunMountId = btn.idValue;
					self.doStart(form, processFunMountId, idValue);
				}
			},
			/**
			 * 提交操作，展示委托人的信息
			 */
			"borrowform button[ref=wfNext]" : {
				click : function(btn) {
					this.showTaskWindow(btn);
				}
			},
			/**
			 * 提交操作，展示委托人的信息
			 */
			"borrowform button[ref=wfBack]" : {
				click : function(btn) {
					this.showTaskWindow(btn);
				}
			},
			/**
			 * 取回操作
			 */
			"borrowform button[ref=wfReturn]" : {
				click : function(btn) {
					var form=btn.up("borrowform");
					var formObj=btn.up("borrowform").getForm();
					var modelId=formObj.findField("id").getValue();
					self.doSubmit(btn,"com.oa.model.BorrowMoney",modelId,{form:form,modelName:"com.oa.model.BorrowMoney",idName:"id"});
//					self.resetFormInfo(form);
//					self.loadInfo(form, "com.oa.model.BorrowMoney",modelId,{form:form,modelName:"com.oa.model.BorrowMoney",idName:"id"});
				}
			},
			/**
			 * 撤销操作
			 */
			"borrowform button[ref=wfEnd]" : {
				click : function(btn) {
					var form=btn.up("borrowform");
					var formObj=btn.up("borrowform").getForm();
					var modelId=formObj.findField("id").getValue();
					self.doCall(btn,"com.oa.model.BorrowMoney",modelId,{form:form,modelName:"com.oa.model.BorrowMoney",idName:"id"});
				}
			},
			/**
			 *  处理任务操作
			 */
			"borrowform button[ref=wfTake]" : {
				click : function(btn) {
					var form=btn.up("borrowform");
					self.doTakeTask(btn,{form:form,modelName:"com.oa.model.BorrowMoney",idName:"id"});
				}
			},
			/**
			 * 送交操作---弹出
			 */
			"wftaskwindow panel[ref=taskjob] button[ref=wfNext]":{
				click:function(btn){
					var form=Ext.getCmp("borrowform");
					var formObj=form.getForm();
					var modelId=formObj.findField("id").getValue();
					self.doSubmit(btn,"com.oa.model.BorrowMoney",modelId,{form:form,modelName:"com.oa.model.BorrowMoney",idName:"id"});
				}
			},
			/**
			 * 回退操作
			 */
			"wftaskwindow panel[ref=taskjob] button[ref=wfBack]":{
				click:function(btn){
					var form=Ext.getCmp("borrowform");
					var formObj=form.getForm();
					var modelId=formObj.findField("id").getValue();
					self.doSubmit(btn,"com.oa.model.BorrowMoney",modelId,{form:form,modelName:"com.oa.model.BorrowMoney",idName:"id"});
				}
			}
		});
	},
	views : ["core.oa.view.BorrowForm", "core.oa.view.BorrowGrid",
			"core.oa.view.BorrowLayout", "core.oa.view.RoleUserTree",
		    "core.oa.view.WfTaskSubmitWindow","core.oa.view.TaskConments"],
	stores : ["core.oa.store.BorrowMoneyStore", "core.oa.store.RoleUserStore"],
	models : ["core.oa.model.BorrowMoneyModel", "core.oa.model.RoleUserModel"]
});